<?
include("includes/functions.php");
define('SITE_AREA', 'home');

// Check for login
if ($_POST[login] == 1) {
    // Check obvious stuff
    if (empty($_POST[userid]) || empty($_POST[password])) {
		$error = 1;
		$message = "<font color=red>You must enter a User Name and Password.</b></font>";
	}
	if (empty($error)) {
		// Check against dbase
		$sql = "SELECT * from casa_users WHERE Userid = '$_POST[userid]' AND Password = '$_POST[password]'";
		$result = mysql_db_query($dbname,$sql);
		$numRows = mysql_num_rows($result);
		if (empty($numRows)) {
		    $error = 1;
		    $message = "<font color=red><b>Sorry!</b> I can't find your login information. Please try again.</font>";
		}
	}
	if (empty($error)) {
		// Login and redirect
	    $row = mysql_fetch_assoc($result);
	       
	    $_SESSION['UID']    = $row[UID];
	    $_SESSION['userid'] = $row[Userid];
	    $_SESSION['name']   = $row[NameFirst].' '.$row[NameLast];
	    $_SESSION['userLevel'] = $row[AdminLevel];
	    $_SESSION['programID'] = $row[ProgramID];
	    $_SESSION['logged'] = 1;	
	}
}

// Logout
if ($_GET[logout] == 1) {
	session_start();
	session_unset();
	session_destroy();
	
	// Redirect
	header("Location: index.php");
}
if (empty($login)) {
	$bodyStuff = "onLoad = 'document.loginForm.userid.focus()';";
}
include("includes/header.php"); ?>

<div id="autoTabs" class="x-layout-inactive-content"></div>
  <div id="content0">
        <?
		if ($_SESSION[logged] != 1) {
		 // Display login box
		?>
		
		<div id="subNoLogin"></div>
		<div id="pageHeader" style="margin-left: 200px;"><h1>Please Login</b></h1></div>
		<?
		if (!empty($error)) {
			echo "<div id=\"pageInstructions\">$message</div>\n";
		}
		?>
		<div class="emphBox" id="form" style="width: 400px;">
			<div class="emphBoxInner">
			<form action="<?=$PHP_SELF; ?>" method="POST" name="loginForm">
			<input type="hidden" name="login" value="1">
				<p><label for="userid">User Name</label></p>
				<div class="formFieldBG"><input type="text" name="userid" id="userid" value="<?=$userid; ?>" size="20" /></div>
				<p><label for="password">Password</label></p>
				<div class="formFieldBG"><input type="password" name="password" id="password" size="20" /></div>
			
				<div align="center"><input type="image" src="images/login.gif" style="border: 0px;" title="Login" /></div>
			</form>
			</div>	
		</div>		
		<? } else { ?>
		<div style="margin-left: 5px; width: 100%;">
			<div class="pageLeftContent"></div>
			</div>	
			<div id="pageContent" style="margin-left: 200px;">
				<div id="pageHeader">	
					<h1>Welcome <?=$_SESSION[name]; ?></h1>
					Select a menu option at left to get started.
				
				</div>
				<div id="resourcePath"></div>
				<div id="resourceList"></div>
			</div>
			<div style="clear: left;"><br /></div>		
		</div>		
		<? } ?>
  </div>
  <div id="content2"></div>
  <div id="east"></div>
	
  <div id="props-panel" class="x-layout-inactive-content" style="width:200px;height:200px;overflow:hidden;">
  </div>
  <div id="south" class="x-layout-inactive-content">
    south - generally for informational stuff, also could be for status bar
  </div>
<? include("includes/footer.php"); ?>